package com.tian.config;

import com.tian.constant.RabbitMQConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * {@code @description:} rabbitMQ
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-03-24 9:17
 * {@code @version:} 1.0
 */
@Configuration
public class RabbitConfig {
    //发送短信
    @Bean
    public Queue queueMessageSendSms() {
        return new Queue(RabbitMQConstant.SMS_QUEUE,true);
    }

    @Bean
    TopicExchange exchangeSendSms() {
        return new TopicExchange(RabbitMQConstant.SMS_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeSendSms(Queue queueMessageSendSms, TopicExchange exchangeSendSms) {
        return BindingBuilder.bind(queueMessageSendSms).to(exchangeSendSms).with(RabbitMQConstant.SMS_ROUTING_KEY);
    }

    //发送站内信
    @Bean
    public Queue queueMessageSendSysMessage() {
        return new Queue(RabbitMQConstant.SYS_MESSAGE_QUEUE,true);
    }

    @Bean
    TopicExchange exchangeSendSysMessage() {
        return new TopicExchange(RabbitMQConstant.SYS_MESSAGE_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeSendSysMessage(Queue queueMessageSendSysMessage, TopicExchange exchangeSendSysMessage) {
        return BindingBuilder.bind(queueMessageSendSysMessage).to(exchangeSendSysMessage).with(RabbitMQConstant.SYS_MESSAGE_ROUTING_KEY);
    }
    //发送邮件
    @Bean
    public Queue queueMessageSendEmail() {
        return new Queue(RabbitMQConstant.SEND_EMAIL_QUEUE,true);
    }

    @Bean
    TopicExchange exchangeSendEmail() {
        return new TopicExchange(RabbitMQConstant.SEND_EMAIL_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeSendEmail(Queue queueMessageSendEmail, TopicExchange exchangeSendEmail) {
        return BindingBuilder.bind(queueMessageSendEmail).to(exchangeSendEmail).with(RabbitMQConstant.SEND_EMAIL_ROUTING_KEY);
    }
    @Bean
    @Scope("prototype")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置消息投递失败的策略，有两种策略：自动删除或返回到客户端。
        //我们既然要做可靠性，当然是设置为返回到客户端(true是返回客户端，false是自动删除)
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}
